package com.service.Impl;

import com.dao.SysUserMapper;
import com.entity.SysUser;
import com.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**用户模块业务层实现*/
@Transactional
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    //示例13关键代码
    @Autowired //@Resource
    private SysUserMapper sysUserMapper;

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {
            return sysUserMapper.SelectSysUserList(sysUser);
        }catch (RuntimeException e){
            throw e;
        }
    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result = false;
        try{
            if (sysUserMapper.add(sysUser) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public boolean delete(int id) {
        boolean result = false;
        try {
            if (sysUserMapper.delete(id) == 1){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int update(int id, String password) {
        int result = 0;
        try {
            if (sysUserMapper.update(id, password) == 1) {
                result = 1;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }

        return result;
    }

    @Override
    public int updateUser(SysUser sysUser) {
        int result = 0;
        try {
            if (sysUserMapper.updateUser(sysUser) == 1){
                result = 1;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
